<template>
  <div>
    <img alt="Vue logo" src="./assets/logo.png" />
    <!-- <HelloWorld msg="Welcome to Your Vue.js App" /> -->
    <ul>
      <li v-for="items in chapterList" :key="items.chapterId">
        {{ items.chapterTitle }}
      </li>
    </ul>
    <input type="input" v-model="chapter" />
    <button @click="sub">提交</button>
    <el-row>
      <el-button>默认按钮</el-button>
      <el-button type="primary">主要按钮</el-button>
      <el-button type="success">成功按钮</el-button>
      <el-button type="info">信息按钮</el-button>
      <el-button type="warning">警告按钮</el-button>
      <el-button type="danger">危险按钮</el-button>
    </el-row>
    <!-- <el-table :data="chapterList" stripe style="width: 50%">
      <el-table-column prop="timer" label="日期" width="180"> </el-table-column>
      <el-table-column prop="chapterId" label="id" width="180">
      </el-table-column>
      <el-table-column prop="chapterTitle" label="标题"> </el-table-column>
    </el-table> -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="文章标题" width="180">
      </el-table-column>
      <el-table-column prop="province" label="城市" width="180">
      </el-table-column>

      <el-table-column prop="city" label="地区" width="180"> </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >Edit</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >Delete</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// import HelloWorld from "./components/HelloWorld.vue";
import axios from "axios";

export default {
  name: "App",
  components: {
    // HelloWorld,
  },
  data() {
    return {
      msg: "",
      chapterList: [],
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎1",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1518 弄",
          zip: 200333,
        },
        {
          date: "2016-05-04",
          name: "王小虎2",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1517 弄",
          zip: 200333,
        },
        {
          date: "2016-05-01",
          name: "王小虎3",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1519 弄",
          zip: 200333,
        },
        {
          date: "2016-05-03",
          name: "王小虎4",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1516 弄",
          zip: 200333,
        },
      ],
    };
  },
  methods: {
    getList() {
      axios.get("http://localhost:3000/getList").then((res) => {
        let result = res.data;
        this.chapterList = result;
        console.log(result);
      });
    },
    sub() {
      console.log(this.chapter);
      axios
        .post("http://localhost:3000/postsub", { chapter: this.chapter })
        .then((res) => {
          console.log(res);
          let chapterObj = {
            chapterId: this.chapterList.length + 1,
            chapterTitle: this.chapter,
            timer: "2023-3-3",
          };
          this.chapterList.push(chapterObj);
        });
    },
    handleEdit(index, row) {
      console.log(index, row);
      console.log(this.tableData);

      this.tableData.splice(index, 1);
      console.log(this.tableData);
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.tableData.splice(index, 1);
    },
  },
  mounted() {
    axios.get("http://localhost:3000/getdata").then((res) => {
      console.log(res);
      this.msg = res.data;
      console.log(this.msg);
    });
    this.getList();
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
